package com.openess.bigsearch.engine.utils;

import java.util.Iterator;

/**
 * 利用链表实现的优先队列，有头指针以及为指针。
 * 
 * @author TuTu
 * 
 * @param <E>
 */
public class LinkedQueue<E> implements QQueue<E> {
	/**
	 * 队头
	 */
	private Node<E> front;
	/**
	 * 队尾
	 */
	private Node<E> rear;

	/**
	 * 构造方法，初始化队列
	 */
	public LinkedQueue() {
		front = rear = null;
	}

	@Override
	public boolean isEmpty() {
		return front == null && rear == null;
	}

	@Override
	public boolean enqueue(E element) {
		if (element == null) {
			return false;
		}
		Node<E> q = new Node<E>(element);
		if (!isEmpty()) {
			rear.setNext(q);
		} else {
			front = q;
		}
		rear = q;
		return true;
	}

	@Override
	public E dequeue() {
		if (!isEmpty()) {
			E temp = front.getData();
			front = front.getNext();
			// 如果出操作执行后，队列空了，那么将rear设为null
			if (front == null) {
				rear = null;
			}
			return temp;
		} else {
			return null;
		}
	}

	@Override
	public String toString() {
		String str = "queue: ";
		Node<E> p = this.front;
		while (p != null) {
			str = str + p.getData() + "| ";
			p = p.getNext();
		}
		return str;
	}

	@Override
	public Iterator<E> iterator() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean contain(E elements) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public <T> T[] toArray(T[] a) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object[] toArray() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> T[] toArray(T a) {
		// TODO Auto-generated method stub
		return null;
	}
}
